package cn.deschen.search.index;

import cn.deschen.common.config.cache.Cache;

import java.util.Map;

/**
 * @Author hanbin_chen
 * @Description 索引通用方法接口
 * @DateTime 2021/6/5
 * @Version V1.0.0
 */
public class IndexAware<K, V extends IndexObject> {

    public static final String PREFIX_ADVERT_INDEX = "ADVERT:INDEX:";

    protected Cache<String, V> cache;

    private String indexKey;

    public IndexAware(Cache cache, String indexType) {
        this.cache = cache;
        this.indexKey = PREFIX_ADVERT_INDEX + indexType;
    }

    public V get(K key) {
        return cache.hGet(indexKey, key);
    }

    public void add(K key, V value) {
        update(key, value);
    }

    public void update(K key, V value) {
        V oldValue = cache.hGet(indexKey, key);
        if (null == oldValue) {
            cache.hSet(indexKey, key, value);
        }else {
            oldValue.update(value);
        }
    }

    public void delete(K key){
        cache.hDel(indexKey, key);
    };

    public Cache getCache() {
        return cache;
    }
}
